package com.ffcs.crmd.tsp.server.netty.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailRunning;
import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailThreadInfo;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.application.ApplicationManagerHolder;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.job.info.JobDetailShell;
import com.ffcs.crmd.tsp.server.job.info.JobDetailShellChannelPairInfo;
import com.ffcs.crmd.tsp.server.manager.JobProcessManager;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;

/**
 * 
 * 功能说明:创建任务执行者
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年1月21日 上午9:44:55
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class InterruptJobDetaiRunninglProcessor implements INettyProcessor {
    /**
     * 日志
     */
    private final static Logger LOG               = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    /**
     * 通信服务端
     */
    private RemotingHandler     remotingController;
    
    /**
     * 任务端管理器
     */
    private JobProcessManager   jobProcessManager = ApplicationManagerHolder.getBean("jobProcessManager", JobProcessManager.class);
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public InterruptJobDetaiRunninglProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.remotingController = remotingController;
    }
    
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.info("SERVER-NETTY: one request to interrupt job detail is received that body=[{}]", requestXml);
            TspJobDetailRunning tspJobDetailRunningVO = (TspJobDetailRunning) XmlUtil.fromXml(requestXml, TspJobDetailRunning.class);
            JobDetailShellChannelPairInfo jobDetailShellChannelPairInfo = jobProcessManager
                .getJobDetailRunningPairInfo(tspJobDetailRunningVO.getJobName(), tspJobDetailRunningVO.getId());
            if (jobDetailShellChannelPairInfo == null) {
                throw new TspException("this job detail is not running, it may have been completed or transferred to a dead job detail.");
            }
            
            Channel channel = jobDetailShellChannelPairInfo.getChannel();
            JobDetailShell jobDetailShell = jobDetailShellChannelPairInfo.getJobDetailShell();
            boolean result = remotingController.shutdownFinish(jobDetailShell, channel);
            RetVo<TspJobDetailThreadInfo> retVo = new RetVo<TspJobDetailThreadInfo>();
            if (result) {
                retVo.setObject(String.valueOf("true"));
                response.setBody(XmlUtil.toXml(retVo).getBytes());
            } else {
                response.setCode(ResponseCode.FAIL);
                response.setBody("无法关闭或当前执行端版本太低，请更新至最新版本".getBytes());
            }
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query job detail death[{}] exception:", requestXml, e);
            String responseBody = String.format("query job detail death exception:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
}
